﻿namespace BloodlustFury.DataMiner
{
	#region "Using Statements"

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Windows.Forms;
	using BloodlustFury.DataMiner.Business;

	#endregion

	public partial class TestDataForm : Form
	{
		public TestDataForm()
		{
			InitializeComponent();

			if (DataEngine.TestModulesLoaded)
			{
				TestModulesGridView.DataSource = DataEngine.TestModules.Items;
				TestModulesGridView.Refresh();
				toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.TestModules.FileName);
				toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.TestModules.Items.Count);
				
			}
			else
			{
				toolStripStatusLabel.Text = "No file loaded.";
				toolStripRecordCount.Text = "Record Count: 0";
			}

			toolStripSolutionsLoaded.Text = String.Format("Solutions Loaded: {0}", DataEngine.SolutionsLoaded.ToString());
		}

		private void LoadButton_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.TestModulesLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (TestDataOpenFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadTestModules(TestDataOpenFileDialog.FileName);
						DataEngine.ClearStatistics();
						DataEngine.PredictionsGenerated = false;

						toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.TestModules.FileName);
						toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.TestModules.Items.Count);
						toolStripSolutionsLoaded.Text = String.Format("Solutions Loaded: {0}", DataEngine.SolutionsLoaded.ToString());

						TestModulesGridView.DataSource = DataEngine.TestModules.Items;
						TestModulesGridView.Refresh();
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading test modules:\n{0}", ex.Message),
														"Bloodlust Fury - DataMiner",
														MessageBoxButtons.OK,
														MessageBoxIcon.Error);

						return;
					}

					MessageBox.Show("Test Modules have been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void LoadSolutionsButton_Click(object sender, EventArgs e)
		{
			DialogResult result = DialogResult.Yes;

			if (DataEngine.SolutionsLoaded)
			{
				result = MessageBox.Show("Loading a new file will delete any existing data you have already generated.\nDo you wish to continue?",
																 "Bloodlust Fury - DataMiner",
																 MessageBoxButtons.YesNo,
																 MessageBoxIcon.Question);
			}

			if (result == DialogResult.Yes)
			{
				if (TestDataOpenFileDialog.ShowDialog() == DialogResult.OK)
				{
					try
					{
						DataEngine.LoadSolutionData(TestDataOpenFileDialog.FileName);
						DataEngine.ClearStatistics();

						toolStripSolutionsLoaded.Text = String.Format("Solutions Loaded: {0}", DataEngine.SolutionsLoaded.ToString());

						TestModulesGridView.DataSource = DataEngine.TestModules.Items;
						TestModulesGridView.Refresh();
					}
					catch (Exception ex)
					{
						MessageBox.Show(String.Format("Error loading solution data:\n{0}", ex.Message),
														"Bloodlust Fury - DataMiner",
														MessageBoxButtons.OK,
														MessageBoxIcon.Error);

						return;
					}

					MessageBox.Show("Solution data has been loaded.", "Bloodlust Fury - DataMiner", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
		}

		private void PredictionsButton_Click(object sender, EventArgs e)
		{
			if (DataEngine.TestModulesLoaded && DataEngine.CaseLibraryLoaded)
			{
				NearestNeighborDialogBox nearestNeighbors = new NearestNeighborDialogBox();

				if (nearestNeighbors.ShowDialog() == DialogResult.OK)
				{
					Cursor = Cursors.WaitCursor;

					DataEngine.FindNearestNeighbor(nearestNeighbors.NearestNeighbors);
					DataEngine.GeneratePredictions();

					TestModulesGridView.Refresh();

					Cursor = Cursors.Default;

					MessageBox.Show("Predictions have been generated.",
													"Bloodlust Fury - DataMiner",
													MessageBoxButtons.OK,
													MessageBoxIcon.Information);
				}
			}
			else
			{
				MessageBox.Show("You must load both a Case Library and Test Modules in order to generate predictions.",
												"Bloodlust Fury - DataMiner",
												MessageBoxButtons.OK,
												MessageBoxIcon.Information);

			}
		}

		private void TestDataForm_Load(object sender, EventArgs e)
		{
			DataEngine.OnTestModulesLoaded += this.OnTestModulesLoaded;
			DataEngine.OnPredictionsGenerated += this.OnPredictionsGenerated;
			DataEngine.OnSolutionsLoaded += this.OnSolutionsLoaded;
		}

		private void OnTestModulesLoaded(object sender, EventArgs args)
		{
			TestModulesGridView.DataSource = DataEngine.TestModules.Items;
			TestModulesGridView.Refresh();

			toolStripStatusLabel.Text = String.Format("Loaded: {0}", DataEngine.TestModules.FileName);
			toolStripRecordCount.Text = String.Format("Record Count: {0}", DataEngine.TestModules.Items.Count);
		}

		private void OnSolutionsLoaded(object sender, EventArgs args)
		{
			if (DataEngine.TestModulesLoaded)
			{
				TestModulesGridView.DataSource = DataEngine.TestModules.Items;
				TestModulesGridView.Refresh();

				toolStripSolutionsLoaded.Text = String.Format("Solutions Loaded: {0}", DataEngine.SolutionsLoaded.ToString());
			}
		}

		private void OnPredictionsGenerated(object sender, EventArgs args)
		{
			TestModulesGridView.DataSource = DataEngine.TestModules.Items;
			TestModulesGridView.Refresh();
		}

		private void TestDataForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			DataEngine.OnTestModulesLoaded -= this.OnTestModulesLoaded;
			DataEngine.OnPredictionsGenerated -= this.OnPredictionsGenerated;
			DataEngine.OnSolutionsLoaded -= this.OnSolutionsLoaded;
		}
	}
}
